import { tcccSDKURL } from '../api/index';
function injectTcccWebSDK(SdkURL) {
    if (window.tccc) {
        console.warn('已经初始化SDK了，请确认是否重复执行初始化');
        return;
    }
    return new Promise((resolve, reject) => {
        const script = document.createElement('script');
        script.setAttribute('crossorigin', 'anonymous');
        script.src = SdkURL;
        document.body.appendChild(script);

        script.addEventListener('load', () => {
            // 加载JS SDK文件成功，此时可使用全局变量"tccc"
            window.tccc.on(window.tccc.events.ready, () => {
                /**
                 * Tccc SDK初始化成功，此时可调用外呼等功能。
                 * 注意⚠️：请确保只初始化一次SDK
                 * */
                resolve('初始化成功');
            });
            window.tccc.on(window.tccc.events.tokenExpired, ({ message }) => {
                console.error('初始化失败', message);
                reject(message);
            });
        });
    });
}
function TelSDKURL() {
    tcccSDKURL().then(res => {
        if (res.code == 0) {
            injectTcccWebSDK(res.resp.sdkURL);
        } else {
            console.log('SDK错误');
        }
    });
}
export default TelSDKURL;
